<?php
namespace user\service;

use api\utils\MpeApiClient;
use herosphp\bean\Beans;
use herosphp\utils\AjaxResult;
use herosphp\utils\ArrayUtils;
use user\service\interfaces\IMemberService;
use herosphp\core\Loader;

Loader::import('user.service.interfaces.IMemberService');

/**
 * MPE终端会员服务接口实现
 * @package user\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class MemberService implements IMemberService {


    /**
     * 获取终端会员列表
     * @param $conditions array 查询条件
     * @param $order string 排序方式
     * @param $page int 页码
     * @param $pagesize int 每页条数
     * @return AjaxResult
     */
    public function getMembers(array $conditions, $order, $page, $pagesize)
    {
        $params = array(
            'conditions' => $conditions,
            'order' => $order,
            'page' => $page,
            'pagesize' => $pagesize
        );
        $url = "api/memberApi/getMembers";
        $resVo = MpeApiClient::send($url, $params);
        $items = $resVo->getItems();
        if ( $resVo->isSucess() && !empty($items) ) {
            //填充公司名称
            $mpeService = Beans::get('app.mpe.service');
            $mpeIds = array();
            foreach ( $items as $value ) {
                $mpeIds[] = $value['app_id'];
            }
            $mpes = $mpeService->getItems(array('id' => array('$in' => $mpeIds)));
            $mpes = ArrayUtils::changeArrayKey($mpes, 'id');

            foreach ( $items as $key => $v ) {
                $items[$key]['company'] = $mpes[$v['app_id']]['company'];
            }

        }
        $resVo->putItems($items);
        return $resVo;
    }


    /**
     * 获取会员的资金信息列表
     * @param $conditions array 查询条件
     * @param $order string 排序方式
     * @param $page int 页码
     * @param $pagesize int 每页条数
     * @return AjaxResult
     */
    public function getMemberFunds(array $conditions, $order, $page, $pagesize)
    {
        $conditions['sign_status'] = 1; //只获取已签约用户
        $params = array(
            'conditions' => $conditions,
            'order' => $order,
            'page' => $page,
            'pagesize' => $pagesize
        );
        $url = "api/memberApi/getMembers";
        $resVo = MpeApiClient::send($url, $params);
        $items = $resVo->getItems();
        if ( $resVo->isSucess() && !empty($items) ) {

            $userIds = array();
            foreach ( $items as $val ) {
                $userIds[] = $val['dasu_pay_id'];
            }
            //获取用户的账户信息
            $userService = Beans::get('user.user.service');
            $users = $userService->getItems(array('id' => array('$in' => $userIds)));
            $users = ArrayUtils::changeArrayKey($users, 'id');
            foreach ( $items as $key => $value ) {
                $items[$key]['dasu_username'] = $users[$value['dasu_pay_id']]['username'];
                $items[$key]['balance'] = $users[$value['dasu_pay_id']]['balance'];
                $items[$key]['usable'] = $users[$value['dasu_pay_id']]['usable'];
                $items[$key]['total_cost'] = $users[$value['dasu_pay_id']]['total_cost'];
                $items[$key]['company'] = $users[$value['dasu_pay_id']]['company'];
                $items[$key]['total_cost'] = $users[$value['dasu_pay_id']]['total_cost'];
                $items[$key]['bank_id'] = $users[$value['dasu_pay_id']]['sign_bank_id'];
                $items[$key]['card_no'] = $users[$value['dasu_pay_id']]['card_no'];
                $items[$key]['virtual_account'] = $users[$value['dasu_pay_id']]['virtual_account'];
            }


        }
        $resVo->putItems($items);
        return $resVo;
    }
}
